<template>
  <el-container>
    <el-header />
    <el-main>
      <el-col :span="1"><div class="grid-content bg-purple-light">
        <el-button class="table-header-btn" type="primary" icon="el-icon-plus" size="mini" @click="addDictionaryDirClick">新增</el-button>
      </div></el-col>
      <template>
        <el-table :data="dictionary_dirs" stripe border style="width: 100%">
          <el-table-column prop="name" label="字典名称" width="180" />
          <el-table-column prop="code" label="字典唯一编码" />
          <el-table-column
            fixed="right"
            label="操作"
            width="180"
          >
            <template slot-scope="scope">
              <el-button type="primary" size="mini" @click="lookDictionaryParamsClick(scope.row)">字典项</el-button>
              <el-button type="danger" size="mini" @click="deleteDictionaryDirClick(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </el-main>

    <el-footer>
      <div class="block">
        <el-pagination
          :current-page="page.current"
          :page-sizes="[5,10, 20, 50, 100]"
          :page-size="page.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-footer>

    <el-dialog title="字典项" :visible.sync="dialogDirectoryParamsVisible" :close-on-click-modal="false" center width="70%" append-to-body>
      <dictionary_params :dictionary-dir="currentDictionaryDir" />
    </el-dialog>

    <el-dialog title="字典目录添加" :visible.sync="dialogFormVisible" :close-on-click-modal="false" center append-to-body>
      <el-form :model="tempDirectoryDir" label-width="130px" :inline="true">
        <el-form-item label="目录名称">
          <el-input v-model="tempDirectoryDir.name" />
        </el-form-item>
        <el-form-item label="目录唯一编码">
          <el-input v-model="tempDirectoryDir.code" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addDirectoryDirTask">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="删除确认"
      :visible.sync="dialogConfirmVisible"
      :close-on-click-modal="false"
      width="30%"
      append-to-body
      center
    >
      <span>{{ deleteMessage }}</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogConfirmVisible = false">取 消</el-button>
        <el-button type="danger" @click="deleteDirectoryDirTask">确 定</el-button>
      </span>
    </el-dialog>

  </el-container>
</template>
<script>
import request from '@/utils/request'
import dictionary_params from './modules/index'
export default {
  components: {
    dictionary_params
  },
  data: function() {
    return {
      page: {
        current: 1,
        size: 5,
        total: 0
      },
      deleteMessage: '确认删除该字典目录及字典项吗？',
      deleteDirectoryDir: {},
      tempDirectoryDir: {},
      dictionary_dirs: [],
      dialogDirectoryParamsVisible: false,
      dialogConfirmVisible: false,
      dialogFormVisible: false,
      currentDictionaryDir: {}
    }
  },
  created() {
    this.query()
  },
  methods: {
    // 每页显示数目发生变化时
    handleSizeChange: function(val) {
      this.page.size = val
      this.page.current = 1
      this.query()
    },
    // 当前页发生变化时
    handleCurrentChange: function(val) {
      this.page.current = val
      this.query()
    },
    // 字典目录查询
    query: function() {
      this.$progress.start()
      request({
        url: '/preset/dictionary_dir/query',
        method: 'post',
        data: this.page
      }).then(res => {
        this.$progress.done()
        if (res.result) {
          this.page.total = res.data.total
          this.dictionary_dirs = res.data.data
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => { this.$progress.done() })
    },
    // 查看字典项
    lookDictionaryParamsClick: function(row) {
      this.currentDictionaryDir = row
      this.dialogDirectoryParamsVisible = true
    },
    // 删除字典事件
    deleteDictionaryDirClick: function(row) {
      this.deleteDirectoryDir = row
      this.dialogConfirmVisible = true
    },
    deleteDirectoryDirTask: function() {
      this.$progress.start()
      request({
        url: '/preset/dictionary_dir/delete',
        method: 'post',
        data: this.deleteDirectoryDir
      }).then(res => {
        this.$progress.done()
        if (res.result) {
          this.dialogConfirmVisible = false
          this.query()
          this.$message.success(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => { this.$progress.done() })
    },
    // 添加字典目录
    addDictionaryDirClick: function() {
      this.dialogFormVisible = true
    },
    // 添加字典目录任务
    addDirectoryDirTask: function() {
      this.$progress.start()
      request({
        url: '/preset/dictionary_dir/add',
        method: 'post',
        data: this.tempDirectoryDir
      }).then(res => {
        this.$progress.done()
        if (res.result) {
          this.tempDirectoryDir = {}
          this.dialogFormVisible = false
          this.query()
          this.$message.success(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      }
      ).catch(() => { this.$progress.done() })
    }
  }
}
</script>
